package com.google.android.gms.ads;

import android.content.Context;
import android.os.RemoteException;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresPermission;
import com.google.android.gms.ads.doubleclick.PublisherAdRequest;
import com.google.android.gms.ads.formats.NativeAdOptions;
import com.google.android.gms.ads.formats.NativeAppInstallAd;
import com.google.android.gms.ads.formats.NativeContentAd;
import com.google.android.gms.ads.formats.NativeCustomTemplateAd;
import com.google.android.gms.ads.formats.OnPublisherAdViewLoadedListener;
import com.google.android.gms.ads.formats.PublisherAdViewOptions;
import com.google.android.gms.ads.formats.UnifiedNativeAd;
import com.google.android.gms.common.internal.Hide;
import com.google.android.gms.common.internal.zzbq;
import com.google.android.gms.internal.zzaky;
import com.google.android.gms.internal.zzkh;
import com.google.android.gms.internal.zzkn;
import com.google.android.gms.internal.zzko;
import com.google.android.gms.internal.zzlc;
import com.google.android.gms.internal.zzli;
import com.google.android.gms.internal.zzll;
import com.google.android.gms.internal.zzlo;
import com.google.android.gms.internal.zzme;
import com.google.android.gms.internal.zzmu;
import com.google.android.gms.internal.zzqh;
import com.google.android.gms.internal.zzrs;
import com.google.android.gms.internal.zzrv;
import com.google.android.gms.internal.zzsh;
import com.google.android.gms.internal.zzsq;
import com.google.android.gms.internal.zzsr;
import com.google.android.gms.internal.zzss;
import com.google.android.gms.internal.zzst;
import com.google.android.gms.internal.zzsu;
import com.google.android.gms.internal.zzsw;
import com.google.android.gms.internal.zzwe;

public class AdLoader {
    private final Context mContext;
    private final zzkn zzaln;
    private final zzll zzalo;

    public static class Builder {
        private final Context mContext;
        private final zzlo zzalp;

        private Builder(Context context, zzlo zzlo) {
            this.mContext = context;
            this.zzalp = zzlo;
        }

        public Builder(Context context, String str) {
            this((Context) zzbq.checkNotNull(context, "context cannot be null"), zzlc.zzik().zzb(context, str, new zzwe()));
        }

        public AdLoader build() {
            try {
                return new AdLoader(this.mContext, this.zzalp.zzdi());
            } catch (RemoteException e) {
                zzaky.zzb("Failed to build AdLoader.", e);
                return null;
            }
        }

        public Builder forAppInstallAd(NativeAppInstallAd.OnAppInstallAdLoadedListener onAppInstallAdLoadedListener) {
            try {
                this.zzalp.zza((zzrs) new zzsq(onAppInstallAdLoadedListener));
                return this;
            } catch (RemoteException e) {
                zzaky.zzc("Failed to add app install ad listener", e);
                return this;
            }
        }

        public Builder forContentAd(NativeContentAd.OnContentAdLoadedListener onContentAdLoadedListener) {
            try {
                this.zzalp.zza((zzrv) new zzsr(onContentAdLoadedListener));
                return this;
            } catch (RemoteException e) {
                zzaky.zzc("Failed to add content ad listener", e);
                return this;
            }
        }

        public Builder forCustomTemplateAd(String str, NativeCustomTemplateAd.OnCustomTemplateAdLoadedListener onCustomTemplateAdLoadedListener, NativeCustomTemplateAd.OnCustomClickListener onCustomClickListener) {
            try {
                this.zzalp.zza(str, new zzst(onCustomTemplateAdLoadedListener), onCustomClickListener == null ? null : new zzss(onCustomClickListener));
                return this;
            } catch (RemoteException e) {
                zzaky.zzc("Failed to add custom template ad listener", e);
                return this;
            }
        }

        public Builder forPublisherAdView(OnPublisherAdViewLoadedListener onPublisherAdViewLoadedListener, AdSize... adSizeArr) {
            if (adSizeArr == null || adSizeArr.length <= 0) {
                throw new IllegalArgumentException("The supported ad sizes must contain at least one valid ad size.");
            }
            try {
                this.zzalp.zza(new zzsu(onPublisherAdViewLoadedListener), new zzko(this.mContext, adSizeArr));
                return this;
            } catch (RemoteException e) {
                zzaky.zzc("Failed to add publisher banner ad listener", e);
                return this;
            }
        }

        public Builder withAdListener(AdListener adListener) {
            try {
                this.zzalp.zzb((zzli) new zzkh(adListener));
                return this;
            } catch (RemoteException e) {
                zzaky.zzc("Failed to set AdListener.", e);
                return this;
            }
        }

        public Builder withCorrelator(@NonNull Correlator correlator) {
            zzbq.checkNotNull(correlator);
            try {
                this.zzalp.zzb((zzme) correlator.zzbf());
                return this;
            } catch (RemoteException e) {
                zzaky.zzc("Failed to set correlator.", e);
                return this;
            }
        }

        public Builder withNativeAdOptions(NativeAdOptions nativeAdOptions) {
            try {
                this.zzalp.zza(new zzqh(nativeAdOptions));
                return this;
            } catch (RemoteException e) {
                zzaky.zzc("Failed to specify native ad options", e);
                return this;
            }
        }

        public Builder withPublisherAdViewOptions(PublisherAdViewOptions publisherAdViewOptions) {
            try {
                this.zzalp.zza(publisherAdViewOptions);
                return this;
            } catch (RemoteException e) {
                zzaky.zzc("Failed to specify DFP banner ad options", e);
                return this;
            }
        }

        @Hide
        public final Builder zza(UnifiedNativeAd.zza zza) {
            try {
                this.zzalp.zza((zzsh) new zzsw(zza));
                return this;
            } catch (RemoteException e) {
                zzaky.zzc("Failed to add google native ad listener", e);
                return this;
            }
        }
    }

    AdLoader(Context context, zzll zzll) {
        this(context, zzll, zzkn.zzbhz);
    }

    private AdLoader(Context context, zzll zzll, zzkn zzkn) {
        this.mContext = context;
        this.zzalo = zzll;
        this.zzaln = zzkn;
    }

    private final void zza(zzmu zzmu) {
        try {
            this.zzalo.zzd(zzkn.zza(this.mContext, zzmu));
        } catch (RemoteException e) {
            zzaky.zzb("Failed to load ad.", e);
        }
    }

    @Deprecated
    public String getMediationAdapterClassName() {
        try {
            return this.zzalo.zzco();
        } catch (RemoteException e) {
            zzaky.zzc("Failed to get the mediation adapter class name.", e);
            return null;
        }
    }

    public boolean isLoading() {
        try {
            return this.zzalo.isLoading();
        } catch (RemoteException e) {
            zzaky.zzc("Failed to check if ad is loading.", e);
            return false;
        }
    }

    @RequiresPermission("android.permission.INTERNET")
    public void loadAd(AdRequest adRequest) {
        zza(adRequest.zzbe());
    }

    public void loadAd(PublisherAdRequest publisherAdRequest) {
        zza(publisherAdRequest.zzbe());
    }

    @RequiresPermission("android.permission.INTERNET")
    public void loadAds(AdRequest adRequest, int i) {
        try {
            this.zzalo.zza(zzkn.zza(this.mContext, adRequest.zzbe()), i);
        } catch (RemoteException e) {
            zzaky.zzb("Failed to load ads.", e);
        }
    }
}
